<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>分类列表</title>
    <meta name="keywords" content="" />
    <meta name="description" content="">
    <link href="css/bulma.min.css" rel="stylesheet">
    <link href="css/github-markdown.min.css" rel="stylesheet">
    <link rel="stylesheet" href="css/app.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/config.js"></script>
    <script src="js/loginutil.js"></script>

</head>

<body>

    <nav id="navbar" class="navbar has-shadow">
        <div class="container">
            <div class="navbar-brand">
                <div class="navbar-burger burger" data-target="navMenu">
                    <span></span>
                    <span></span>
                    <span></span>
                </div>
            </div>
            <div id="navMenu" class="navbar-menu">
                <div class="navbar-start">
                    <a class="navbar-item" href="index.html">主页</a>
                    <a class="navbar-item" href="about.html">关于</a>
                </div>
                <div class="navbar-end">
                    <div id="login_div" style="display: none;" class="navbar-item">
                        <a id="category_mangager" style="display: none;background-color: #315790;" class="button is-small is-primary"
                           href="category.html">分类管理</a>&nbsp;&nbsp;&nbsp;
                        <a class="button is-small is-primary" style="background-color: #315790;" href="add-article.html">写文章</a>&nbsp;&nbsp;&nbsp;&nbsp;
                        <a id="login_username" href="user-articlelist.html" style="color: #373737"></a>&nbsp;&nbsp;&nbsp;&nbsp;
                        <a href="javascript:logout();" style="color: #373737">退出</a>
                    </div>

                    <div id="unlogin_div" class="navbar-item">
                        <a class="button is-small is-primary" style="background-color: #315790;" href="register.html">注册</a>&nbsp;&nbsp;&nbsp;&nbsp;
                        <a class="button is-small" href="login.html">登录</a>
                    </div>
                </div>
            </div>
        </div>
    </nav>

    <script>
        jQuery(function () {
            var login_username = window.localStorage.getItem("username");
            if (login_username == null) {
                // 未登录
                jQuery("#unlogin_div").show();
            } else {
                // 已经登录
                jQuery("#unlogin_div").hide();
                jQuery("#login_username").html(login_username);
                jQuery("#login_username").attr("href", "user-articlelist.html?uid=" + window.localStorage.getItem("uid"));
                jQuery("#login_div").show();
                // 管理员权限判定
                if (window.localStorage.getItem("manager") == 1) {
                    jQuery("#category_mangager").show();    // 让文章管理按钮显示
                }
            }
        });
    </script>

    <div class="container margin-top">
        <div class="columns">
            <div class="column is-8 is-offset-2">
                <a href="add-category.html" class="button is-small is-primary" style="background-color: #315790;">新建分类</a>
                <table class="table margin-top is-bordered is-striped is-narrow is-hoverable is-fullwidth">
                    <thead>
                        <tr>
                            <th>分类名</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody id="list-tbody">
                        <!-- <tr>
                            <td>Spring</td>
                            <td>
                                <a href="javascript:void(0);" class="button is-small is-danger">删除</a>
                            </td>
                        </tr> -->
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <script>
        // 加载分类列表
        function initList() {
            jQuery.ajax({
                url: req_base_url + "category/list",
                method: "GET",
                headers: {
                    "Authorization": window.localStorage.getItem(token_key)
                },
                data: {},
                success: function (res) {
                    // 将执行的结果展示到前端 {"code": 200, "msg": "状态码描述信息", "date": []}
                    if (res != null && res.code == 200) {
                        // 成功
                        var listHtml = "";
                        for (var i = 0; i < res.data.length; i++) {
                            var category = res.data[i];
                            listHtml += '<tr>';
                            listHtml += '<td>' + category.name + '</td>';
                            listHtml += '<td>';
                            listHtml += '<a href="javascript:doDel(' + category.cid + ');" class="button is-small is-danger">删除</a>';
                            listHtml += '</td>';
                            listHtml += '</tr>'
                        }
                        jQuery("#list-tbody").html(listHtml);
                    } else {
                        // 失败
                        location.href = "error.html";
                        alert("抱歉: 页面加载失败! " + res.msg)
                    }

                }
            })
        }
        initList();


        // 执行删除
        function doDel(cid) {
            if (confirm("确定是否删除!")) {
                // 参数校验
                if (cid == null || cid == "") {
                    location.href = "404.html";
                    alert("删除失败! 参数有误, 请重试");
                    return false;
                }
                jQuery.ajax({
                    url: req_base_url + "category/del",
                    method: "POST",
                    headers: {
                        "Authorization": window.localStorage.getItem(token_key)
                    },
                    data: {
                        "cid": cid
                    },
                    success: function (res) {
                        // 将执行的结果展示到前端 {"code": 200, "msg": "状态码描述信息", "date": []}
                        if (res != null && res.code == 200) {
                            // 成功
                            alert("删除成功!");
                            location.href = location.href;
                        } else {
                            // 失败
                            location.href = "error.html";
                            alert("抱歉: 删除失败失败! " + res.msg)
                        }

                    }
                })
            }
        }
    </script>
</body>

</html>